package com.cenjiawen.common;

import com.jfinal.config.Constants;
import com.jfinal.config.Interceptors;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.zhenyi.tuangou.core.TuangouRoutes;

public class TuangouConfig extends Config {

	public static void main(String[] args) {
		/**
		 * 特别注意：Eclipse 之下建议的启动方式
		 */
		// JFinal.start("ROOT", 9090, "/", 10);
		/**
		 * 特别注意：IDEA 之下建议的启动方式，仅比 eclipse 之下少了最后一个参数
		 */
		try {
			System.out.println("start");
			JFinal.start("ROOT", 9090, "/");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void configConstant(Constants me) {
		super.configConstant(me);
	}

	@Override
	public void configRoute(Routes me) {
		me.add(new TuangouRoutes());
	}

	@Override
	public void configPlugin(Plugins me) {
		super.configPlugin(me);
		/*
		 * super.configPlugin(me); java.util.List<IPlugin> list =
		 * me.getPluginList(); for (IPlugin iPlugin : list) { if (iPlugin
		 * instanceof ActiveRecordPlugin) { } }
		 */
		if (!PropKit.getBoolean("devMode", false)) {
		}
		Cron4jPlugin cp = new Cron4jPlugin("task.txt", "cron4j");
		me.add(cp);
	}

	@Override
	protected void configSQL(ActiveRecordPlugin arp) {
		super.configSQL(arp);
		arp.addSqlTemplate("tg_admin.sql");
	}

	@Override
	public void configInterceptor(Interceptors me) {
	}

	@Override
	protected void configSQLConnection(Plugins me) {
		super.configSQLConnection(me);
		/*String host = "192.168.50.18";
		String scheme = "beikangayi";
		String user = "root";
		String password = "zhenyi";

		String host1 = "192.168.50.141";
		String scheme1 = "demo";
		String user1 = "root";
		String password1 = "123456";*/
		//
		// addDataSource(host1, scheme1, user1, password1, me, new ArpHandler()
		// {
		// @Override
		// public void configMapping(ActiveRecordPlugin arg0) {
		// // TODO Auto-generated method stub
		// arg0.setBaseSqlTemplatePath(PathKit.getRootClassPath());
		// arg0.addSqlTemplate("ws.sql");
		// com.demo.common.model._MappingKit.mapping(arg0);
		// }
		// });

		/*String jdbc = "jdbc:mysql://" + host1 + "/" + scheme1
				+ "?autoReconnect=true&useUnicode=true&characterEncoding=utf-8";
		// 配置C3p0数据库连接池插件
		C3p0Plugin druidPlugin = new C3p0Plugin(jdbc, user1, password1);
		me.add(druidPlugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(scheme1, druidPlugin);
		arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
		arp.addSqlTemplate("ws.sql");
		com.demo.common.model._MappingKit.mapping(arp);
		me.add(arp);*/
	}

	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		// WSServer wsServer = new WSServer();
		// wsServer.start();
		String appId = PropKit.get("appId");
		if (StrKit.isBlank(appId)) {
			return;
		}
		String token = PropKit.get("token");
		String appSecret = PropKit.get("appSecret");
		String encodingAesKey = PropKit.get("encodingAesKey");
		ApiConfig config = new ApiConfig(token, appId, appSecret);
		config.setEncodingAesKey(encodingAesKey);
		config.setEncryptMessage(true);
		ApiConfigKit.putApiConfig(config);
		ApiConfigKit.setThreadLocalAppId(appId);

	}

	@Override
	public void beforeJFinalStop() {
		super.beforeJFinalStop();
	}

}
